package com.boco.modules.fdoc.web.organization;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.taglibs.standard.lang.jstl.test.beans.PublicInterface2;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.model.DoctorEntity;
import com.boco.modules.fdoc.model.DoctorUserEntity;
import com.boco.modules.fdoc.model.HospitalEntity;
import com.boco.modules.fdoc.service.DepartmentService;
import com.boco.modules.fdoc.service.DocService;
import com.boco.modules.fdoc.service.DocUserService;
import com.boco.modules.fdoc.service.HospitalService;
import com.boco.modules.fdoc.vo.DoctorDetailVo;
import com.boco.modules.fdoc.vo.TdepartmentVo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

/**
 * introduction: 医院管理模块
 * @author lzz
 *
 */
@Controller
@RequestMapping(value="/doctor",produces = "application/json;charset=UTF-8")
public class DoctorController {
	
	@Resource
	HospitalService hospitalService;
	@Resource
	DocService docService;
	@Resource
	DepartmentService deptService;
	@Resource
	DocUserService userService;
	
	/**
	 * 跳入主页面，携带初始数据及医院的列表
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showPage",method = RequestMethod.GET)
	public String showPage(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		model.addAttribute("hospitalList2",JsonUtils.getJson(hospitalList));
		return "/organization/doctorInfo";
	}
	
	/**
	 * 根据请求参数获取医院下医生列表
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @param docType 医生类型
	 * @param docName 医生姓名
	 * @param pageIndex 当前页码
	 * @param pageSize 每页数据大小
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getDocList",method = RequestMethod.GET)
	public String getDocList(HttpServletRequest request, Model model,String orgId,String docType,
			String docName,Integer pageIndex,Integer pageSize){
		DoctorDetailVo docVo=new DoctorDetailVo();
		docVo.setDocType(docType);
		docVo.setOrgId(orgId);
		docVo.setDocName(docName);
		int count=docService.getDoctorCount(docVo);
		Page<DoctorDetailVo> page=new Page<DoctorDetailVo>(pageIndex, pageSize);
		docVo.setPage(page);
		List<DoctorDetailVo> list=docService.getDocList(docVo);
		return JsonUtils.getJson(BaseJsonVo.pageList(list, count));
	}
	
	/**
	 * 获取医生的详细信息
	 * @param request
	 * @param model
	 * @param id 医生id
	 * @return
	 */
	@RequestMapping(value = "/getDocbyId",method = RequestMethod.GET)
	public String getDocbyId(HttpServletRequest request, Model model,String id){
		DoctorDetailVo docVo=docService.getDoctorInfo(id);
		model.addAttribute("docVo", docVo);
		return "/organization/doctorDetail";
		
	}
	
	/**
	 * 新增医生 医院的List用于医院的下拉框
	 * @param request
	 * @param model
	 * @return 新增医生页面
	 */
	@RequestMapping(value = "/toAddDoc",method = RequestMethod.GET)
	public String toAddDoc(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		return "/organization/addDoctor";
	}
	
	/**
	 * 获取医院下的科室树数据 (已经停止使用)
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value = "/getDeptTreeDataByZtree",method = RequestMethod.GET)
	@ResponseBody
	public String getDeptTreeDataByZtree(String orgId){
		
		//获取一级科室列表
		TdepartmentVo vo=new TdepartmentVo();
		vo.setOrgId(orgId);
		List<TdepartmentVo> parentDepts=deptService.getHospitalFirstLevelDepts(vo);
		List<Map<String, Object>> nodeList = new ArrayList<Map<String,Object>>();
		//遍历一级科室，封装二级科室列表
		for (TdepartmentVo deptVo : parentDepts) {
			Map<String, Object> parentMap = new HashMap<String, Object>();
			parentMap.put("id", deptVo.getId());
			parentMap.put("name", deptVo.getName());
			parentMap.put("open", true);
			List<TdepartmentVo> children = deptService.getHospitalChildrenDepts(deptVo.getId());
			if(children.size()>0){
				parentMap.put("nocheck", true);
				nodeList.add(parentMap);
				for (TdepartmentVo child : children) {
						Map<String, Object> childMap = new HashMap<String, Object>();
						childMap.put("id", child.getId());
						childMap.put("name", child.getName());
						childMap.put("pId", deptVo.getId());
						nodeList.add(childMap);
					}
			}else{
				parentMap.put("nocheck",false);
				nodeList.add(parentMap);
			}
		}
		System.out.println(JsonUtils.getJson(nodeList));
		return JsonUtils.getJson(nodeList);
	}
	
	/**
	 * 保存医生的数据
	 * @param request
	 * @param model
	 * @param docName 医生姓名
	 * @param phoneNumber 电话号
	 * @param orgId 医院
	 * @param title 职称
	 * @param sex 性别
	 * @param specialty 特长
	 * @param introduction 简介
	 * @param deptName 科室
	 * @param docType 医生类型
	 * @param userName 该医生的账号
	 * @param password 该医生的密码
	 * @param docId 医生id
	 * @param docUserId 医生的账户id
	 * @param img 医生头像
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/saveDoctor",method = RequestMethod.POST)
	public String saveDoctor(HttpServletRequest request, Model model,String docName,String phoneNumber,
			String orgId,String title,String sex,String specialty,String introduction,String deptName,
			String docType,String userName,String password,String docId,String docUserId, String img){
		if(docId==null || "".equals(docId)){
			docId=UUID.randomUUID().toString().replace("-", "");
		}
		if(docUserId==null || "".equals(docUserId)){
			docUserId=UUID.randomUUID().toString().replace("-", "");
		}
		String regionCode=hospitalService.getHospatal(orgId).getRegionCode();
		
		DoctorEntity docEntity=new DoctorEntity();
		docEntity.setId(docId);
		docEntity.setDocName(docName);
		docEntity.setPhoneNumber(phoneNumber);
		docEntity.setOrgId(orgId);
		docEntity.setTitle(title);
		docEntity.setSex(sex);
		docEntity.setSpecialty(specialty);
		docEntity.setIntroduction(introduction);
		docEntity.setDeptName(deptName);
		docEntity.setDocType(docType);
		docEntity.setCreateTime(new Date());
		docEntity.setUpdateTime(new Date());
		docEntity.setRegionCode(regionCode);
		
		DoctorUserEntity userEntity=new DoctorUserEntity();
		userEntity.setDoctorUserId(docUserId);
		userEntity.setDoctorId(docId);
		userEntity.setCreateTime(new Date());
		userEntity.setUpdateTime(new Date());
		userEntity.setUserName(userName);
		userEntity.setPassword(password);
		userEntity.setImg(img);
		
		int flag=docService.saveDoctorAndUser(docEntity,userEntity);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	
	/**
	 * 删除医生
	 * @param request
	 * @param model
	 * @param id 医生id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteDoc",method = RequestMethod.POST)
	public String deleteDoc(HttpServletRequest request, Model model,String id){
		
		int flag=docService.deleteDoc(id);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	
	/**
	 * 跳转修改医生信息页面
	 * @param request
	 * @param model
	 * @param id 医生id
	 * @return
	 */
	@RequestMapping(value="/toUpdateDoc",method = RequestMethod.GET)
	public String toUpdateDoc(HttpServletRequest request, Model model,String id){
		//获取此医生详细信息
		DoctorDetailVo docVo=docService.getDoctorInfo(id);
		model.addAttribute("docVo", docVo);
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		model.addAttribute("hospitalList", hospitalList);
		return "/organization/updateDoctor";
		
	}
	
	/**
	 * 修改医生信息
	 * @param request
	 * @param model
	 * @param id 医生id
	 * @param docName 医生姓名
	 * @param phoneNumber 医生电话号
	 * @param orgId 医院id
	 * @param title 职称
	 * @param sex 性别
	 * @param specialty 特长
	 * @param introduction 简介
	 * @param deptName 科室
	 * @param docType 医生类型
	 * @param userName 医生的账号
	 * @param password 医生的密码
	 * @param img 医生头像
	 * @return
	 */
	@Transactional
	@ResponseBody
	@RequestMapping(value = "/updateDoctor",method = RequestMethod.POST)
	public String updateDoctor(HttpServletRequest request, Model model,String id,String docName,String phoneNumber,
			String orgId,String title,String sex,String specialty,String introduction,String deptName,
			String docType,String userName,String password, String img){
		String regionCode=hospitalService.getHospatal(orgId).getRegionCode();
		
		DoctorEntity docEntity=new DoctorEntity();
		docEntity.setId(id);
		docEntity.setDocName(docName);
		docEntity.setPhoneNumber(phoneNumber);
		docEntity.setOrgId(orgId);
		docEntity.setTitle(title);
		docEntity.setSex(sex);
		docEntity.setSpecialty(specialty);
		docEntity.setIntroduction(introduction);
		docEntity.setDeptName(deptName);
		docEntity.setDocType(docType);
		docEntity.setUpdateTime(new Date());
		docEntity.setRegionCode(regionCode);
		
		DoctorUserEntity userEntity=new DoctorUserEntity();
		userEntity.setUserName(userName);
		userEntity.setPassword(password);
		userEntity.setUpdateTime(new Date());
		userEntity.setDoctorId(id);;
		userEntity.setImg(img);
		
		int flag=userService.updateDocAndUser(docEntity, userEntity);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
}
